home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #12 / Amiga Plus CD - 2002 - No. 12.iso / AmigaOS / Aplus_Dev / AP-Website / download / pgp / pgp5gui-174b.lha / PGP5GUI-Src.lha / PGP5GUI-Src / FileReqPGP5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-23  |  8.2 KB  |  343 lines

  1. /*
  2. ** PGP5GUI - A GUI using Magic User Interface v3.8
  3. **
  4. ** Copyright 23-JUNE-1998 by Stefan Zakarias, All Rights Reserved.
  5. **
  6. ** This source code is released as FREEWARE - Use it for whatever you like,
  7. ** as long as NO financial reward is gained by you for such usage.
  8. **
  9. ** If you use any parts of the this source code for anything, give ME credit
  10. ** wherever credit is due, please ;-)
  11. */
  12.  
  13. /* System */
  14. #include <exec/types.h>
  15. #include <exec/libraries.h>
  16.  
  17. /* Libraries */
  18. #include <libraries/asl.h>
  19. #include <libraries/mui.h>
  20.  
  21. /* Prototypes */
  22. #ifdef __GNUC__
  23. #include <proto/alib.h>
  24. #endif
  25.  
  26. #include <proto/muimaster.h>
  27. #include <proto/exec.h>
  28. #include <clib/alib_protos.h>
  29. #include <proto/dos.h>
  30. #include <proto/asl.h>
  31.  
  32. /*  Ansi  */
  33. #include <string.h>
  34.  
  35. #include "PGP5GUI.h"
  36.  
  37. /* Initial values for ASL file requester */
  38. #define MYLEFTEDGE 10
  39. #define MYTOPEDGE  20
  40. #define MYWIDTH    310
  41. #define MYHEIGHT   180
  42.  
  43. extern struct Window *win;
  44. extern struct WinConfigs WinConfig;
  45. extern struct FileRequester *filerequester;
  46.  
  47. extern char *ReadyMSG;
  48.  
  49. extern char *encpath, *encfile, *encfilename;
  50. extern char *decpath, *decfile, *decfilename;
  51. extern char *sigpath, *sigfile, *sigfilename;
  52. extern char *addkeyspath, *addkeysfile, *addkeysfilename;
  53.  
  54. extern char *cwdscratch;
  55. extern char *PGP_path;
  56. extern char *warning_str;
  57. extern char *yesno_str;
  58.  
  59. /* Our "tags" array for the ASL file requester initialization */
  60. ULONG frtags[] =
  61. {
  62.     ASLFR_TitleText,        (ULONG) "\0",
  63.     ASLFR_InitialDrawer,    (ULONG) "\0",
  64.     ASLFR_InitialFile,        (ULONG) "\0",
  65.     ASLFR_Window,            0,
  66.     ASLFR_InitialHeight,    MYHEIGHT,
  67.     ASLFR_InitialWidth,        MYWIDTH,
  68.     ASLFR_InitialLeftEdge,    MYLEFTEDGE,
  69.     ASLFR_InitialTopEdge,    MYTOPEDGE,
  70.     ASLFR_DoSaveMode,        FALSE,
  71.     ASLFR_PositiveText,        (ULONG)"OK",
  72.     ASLFR_NegativeText,        (ULONG)"Cancel",
  73.     TAG_DONE
  74. };
  75.  
  76. /*
  77. ** Returns:
  78. **     FALSE (0) if FileRequester error or user clicked Close/Cancel.
  79. **     TRUE (-1) if user clicked OK or double-clicked a file.
  80. */
  81. BOOL
  82. GetASLFileName(struct ObjApp *App, ULONG hail, ULONG pathbuff, ULONG filebuff)
  83. {
  84.     BOOL result = FALSE;
  85.  
  86.     set(App->TX_Status, MUIA_Text_Contents, "\33cWaiting for file choice");
  87.  
  88.     /* Make the parent window go to sleep */
  89.     set(App->WI_Main, MUIA_Window_Sleep, TRUE);
  90.  
  91.     /* Set some values for our file requester */
  92.     frtags[1] = hail;
  93.     frtags[3] = pathbuff;
  94.     frtags[5] = filebuff;
  95.     frtags[7] = (ULONG) win;
  96.     frtags[9] =  WinConfig.asl_Height;
  97.     frtags[11] = WinConfig.asl_Width;
  98.     frtags[13] = WinConfig.asl_LeftEdge;
  99.     frtags[15] = WinConfig.asl_TopEdge;
  100.  
  101.     /* Open the ASL file requester */
  102.     if (AslRequest(filerequester, (struct TagItem *)frtags))
  103.     {
  104.         if (*(filerequester->rf_File))
  105.         {
  106.             /*
  107.             ** User clicked "OK" and we have a filname...
  108.             ** Get copies of path and file names
  109.             */
  110.             strcpy((char *) pathbuff, filerequester->rf_Dir);
  111.             strcpy((char *) filebuff, filerequester->rf_File);
  112.  
  113.             /* Set return value */
  114.             result = TRUE;
  115.         }
  116.         else
  117.         {
  118.             /* Let the user know that no FILE was selected */
  119.             DisplayBeep(0);
  120.         }
  121.     }
  122.  
  123.     /*
  124.     ** User-friendly stuff...
  125.     ** Save size and position settings of file requester for next use!
  126.     */
  127.     WinConfig.asl_Height = (ULONG) filerequester->fr_Height;
  128.     WinConfig.asl_Width = (ULONG) filerequester->fr_Width;
  129.     WinConfig.asl_LeftEdge = (ULONG) filerequester->fr_LeftEdge;
  130.     WinConfig.asl_TopEdge = (ULONG) filerequester->fr_TopEdge;
  131.  
  132.     /* Make the parent window wake up */
  133.     set(App->WI_Main, MUIA_Window_Sleep, FALSE);
  134.  
  135.     /* Wake up the parent window */
  136.     set(App->WI_Main, MUIA_Window_Activate, TRUE);
  137.  
  138.     set(App->TX_Status, MUIA_Text_Contents, ReadyMSG);
  139.  
  140.     /* How d'we go? */
  141.     return(result);
  142. }
  143.  
  144. /*
  145. ** Returns:
  146. **     FALSE (0) if FileRequester error or user clicked Close/Cancel.
  147. **     TRUE (-1) if user clicked OK or double-clicked a file.
  148. */
  149. BOOL
  150. GetSaveASLFileName(struct ObjApp *App, char *hail, char *pathbuff, char *filebuff, char *tofile)
  151. {
  152.     BOOL result = FALSE;
  153.  
  154.     /* Set ASL Requester tag for DoSaveMode (Save file mode) */
  155.     frtags[17] = TRUE;
  156.  
  157. tryagain:
  158.     /* Get name of file to save key to... And save it! */
  159.     if (GetASLFileName(App, (ULONG) hail, (ULONG) pathbuff, (ULONG) filebuff))
  160.     {
  161.         /*
  162.         ** AddPart() mucks up 'pathbuff[]' by adding 'filebuff[]'
  163.         ** to it.  This prevents us keeping the last path visited
  164.         ** as default for the next use of the ASL file requester.
  165.         ** So, we get a copy of it instead and mess with that!
  166.         */
  167.         strcpy(tofile, pathbuff);
  168.  
  169.         if (AddPart(tofile, filebuff, 512))
  170.         {
  171.             BPTR lock = Lock(tofile, ACCESS_READ);
  172.  
  173.             /* Does file exist? */
  174.             if (lock)
  175.             {
  176.                 /* File exists! */
  177.  
  178.                 /* Finished with lock */
  179.                 UnLock(lock);
  180.  
  181.                 /* Overwrite file? */
  182.                 if (!MUI_Request(App->App, App->WI_Main, 0, warning_str, yesno_str,
  183.                                 "File Exists!\nDo you want to overwrite it?")
  184.                     )
  185.                     goto tryagain;    /* Oooer... A goto! */
  186.             }
  187.  
  188.                result = TRUE;
  189.         }
  190.     }
  191.  
  192.     /* Set ASL Requester tag for DoSaveMode (Load file mode) */
  193.     frtags[17] = FALSE;
  194.  
  195.     return(result);
  196. }
  197.  
  198. void
  199. JoinNameParts(char *fullpath, char *pathbuff, char *filebuff)
  200. {
  201.     strcpy(fullpath, pathbuff);
  202.     AddPart(fullpath, filebuff, 512);
  203. }
  204.  
  205. void
  206. StringToPathFile(char *fullpath, char *pathbuff, char *filebuff)
  207. {
  208.     char *path;
  209.  
  210.     /* Set the default paths for the various ASL Requesters */
  211.     if (*fullpath)
  212.     {
  213.         strcpy(cwdscratch, fullpath);
  214.  
  215.         /* Get the beginning of the file name */
  216.         path = PathPart(cwdscratch);
  217.  
  218.         /* If we're at a directory name... */
  219.         if (*path == '/')
  220.         {
  221.             /* Copy the file name - after the '/' */
  222.             strcpy(filebuff, path+1);
  223.         }
  224.         else
  225.         {
  226.             /* Copy the file name - after the ':' */
  227.             strcpy(filebuff, path);
  228.         }
  229.  
  230.         /* Null terminate the end of the path name */
  231.         *path = '\0';
  232.  
  233.         /* Copy the path name */
  234.         strcpy(pathbuff, cwdscratch);
  235.     }
  236.     else
  237.     {
  238.         /* Use path to 'PGP:' as default */
  239.         strcpy(pathbuff, PGP_path);
  240.     }
  241. }
  242.  
  243.  
  244. void
  245. Get_Encrypt_Name(struct ObjApp *App)
  246. {
  247.     char *enctest;
  248.  
  249.     /* Title for the file requester */
  250.     char title[] = "Name Of File To Encrypt...";
  251.  
  252.     /* Get any previously saved path and file names */
  253.     get(App->STR_PA_Encrypt_FROMFILE,MUIA_String_Contents,&enctest);
  254.  
  255.     /* Set the default paths for the various ASL Requesters */
  256.     StringToPathFile(enctest, encpath, encfile);
  257.  
  258.     /* Get name of file and save it */
  259.     if (GetASLFileName(App, (ULONG) &title, (ULONG) encpath, (ULONG) encfile))
  260.     {
  261.         if (*encfile)
  262.         {
  263.             JoinNameParts(encfilename, encpath, encfile);
  264.             set(App->STR_PA_Encrypt_FROMFILE,MUIA_String_Contents,encfilename);
  265.         }
  266.     }
  267. }
  268.  
  269. void
  270. Get_Decrypt_Name(struct ObjApp *App)
  271. {
  272.     char *dectest;
  273.  
  274.     /* Title for the file requester */
  275.     char title[] = "File To Decrypt/Verify...";
  276.  
  277.     /* Get any previously saved path and file names */
  278.        get(App->STR_PA_Decrypt_TOFILE,MUIA_String_Contents,&dectest);
  279.  
  280.     /* Set the default paths for the various ASL Requesters */
  281.     StringToPathFile(dectest, decpath, decfile);
  282.  
  283.     /* Get name of file and save it */
  284.     if (GetASLFileName(App, (ULONG) &title, (ULONG) decpath, (ULONG) decfile))
  285.     {
  286.         if (*decfile)
  287.         {
  288.             JoinNameParts(decfilename, decpath, decfile);
  289.             set(App->STR_PA_Decrypt_TOFILE,MUIA_String_Contents,decfilename);
  290.         }
  291.     }
  292. }
  293.  
  294. void
  295. Get_Sign_Name(struct ObjApp *App)
  296. {
  297.     char *sigtest;
  298.  
  299.     /* Title for the file requester */
  300.     char title[] = "Name Of File To Sign...";
  301.  
  302.     /* Get any previously saved path and file names */
  303.     get(App->STR_PA_Sign_POPFILE,MUIA_String_Contents,&sigtest);
  304.  
  305.     /* Set the default paths for the various ASL Requesters */
  306.     StringToPathFile(sigtest, sigpath, sigfile);
  307.  
  308.     /* Get name of file and save it */
  309.     if (GetASLFileName(App, (ULONG) &title, (ULONG) sigpath, (ULONG) sigfile))
  310.     {
  311.         if (*sigfile)
  312.         {
  313.             JoinNameParts(sigfilename, sigpath, sigfile);
  314.             set(App->STR_PA_Sign_POPFILE,MUIA_String_Contents,sigfilename);
  315.         }
  316.     }
  317. }
  318.  
  319. void
  320. Get_KeysAdd_Name(struct ObjApp *App)
  321. {
  322.     char *addkeystest;
  323.  
  324.     /* Title for the file requester */
  325.     char title[] = "Add PGP Key(s) From...";
  326.  
  327.     /* Get any previously saved path and file names */
  328.     get(App->STR_PA_Keys_ADD_KEYFILE,MUIA_String_Contents,&addkeystest);
  329.  
  330.     /* Set the default paths for the various ASL Requesters */
  331.     StringToPathFile(addkeystest, addkeyspath, addkeysfile);
  332.  
  333.     /* Get name of file and save it */
  334.     if (GetASLFileName(App, (ULONG) &title, (ULONG) addkeyspath, (ULONG) addkeysfile))
  335.     {
  336.         if (*addkeysfile)
  337.         {
  338.             JoinNameParts(addkeysfilename, addkeyspath, addkeysfile);
  339.             set(App->STR_PA_Keys_ADD_KEYFILE,MUIA_String_Contents,addkeysfilename);
  340.         }
  341.     }
  342. }
  343.